import time

import socks
from telethon import TelegramClient
from database import get_all_data
from socks import SOCKS4
from settings import ADMINS
import asyncio
# SESSION, API_ID, API_HASH


class Client(TelegramClient):
    def __init__(self, session, api_id, api_hash, is_active, user_id, wallet, phone_number=None):
        super().__init__(session, api_id, api_hash)
        self.id = user_id
        self.wallet = wallet
        self.is_active = bool(is_active)
        self.phone_number = phone_number

    @staticmethod
    def get_url_from_button(message):
        return message.reply_markup.rows[0].buttons[0].url

    async def notify_admins(self, message):
        for admin in ADMINS:
            await self.send_message(admin, message)

    async def find_dialog(self, dialog_title):
        dialogs = await self.get_dialogs()
        for dlg in dialogs:

            if dlg.title == dialog_title:
                return dlg
        return None


def create_clients():
    res = []
    for el in get_all_data():
        session = el[0]
        api_id = el[1]
        api_hash = el[2]
        is_active = el[3]
        wallet = el[4]
        user_id = el[5]
        phone_number = el[6]

        if is_active == 1:
            res.append(Client(session=session, api_id=api_id, api_hash=api_hash, is_active=is_active, wallet=wallet, user_id= user_id, phone_number=phone_number))

    return res


async def process():
    cl = create_clients()
    print(len(cl))
    for el in cl:
        await el.start()
        time.sleep(0.2)
        #await el.send_message("BCH_clickbot  ", "/start")
        await el.disconnect()
        time.sleep(0.2)

if __name__ == '__main__':
    asyncio.run(process())